/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample;

import me.panpf.sketch.SLog;
import me.panpf.sketch.sample.event.Events;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import org.greenrobot.eventbus.EventBus;

/**
 * AppConfig
 */
public class AppConfig {

    private static DatabaseHelper helper;

    /**
     * getBoolean
     * @param context
     * @param key
     * @return boolean
     */
    public static boolean getBoolean(Context context, Key key) {
        return getDefaultPreferences(context).getBoolean(key.keyName, key.isDefaultValue);
    }

    /**
     * putBoolean
     * @param context
     * @param key
     * @param newValue
     */
    public static void putBoolean(Context context, Key key, boolean newValue) {
        getDefaultPreferences(context).putBoolean(key.keyName, newValue).flush();
        EventBus.getDefault().post(new Events.AppConfigChangedEvent(key));
        SampleSketchInitializer.onEvent(new Events.AppConfigChangedEvent(key), context);
    }

    /**
     * getString
     * @param context
     * @param key
     * @return String
     */
    public static String getString(Context context, Key key) {
        return getDefaultPreferences(context).getString(key.keyName, null);
    }

    /**
     * putString
     * @param context
     * @param key
     * @param newValue
     */
    public static void putString(Context context, Key key, String newValue) {
        getDefaultPreferences(context).putString(key.keyName, newValue).flush();

        if (key == Key.LOG_LEVEL) {
            setLogLevel(context);
        }
    }

    private static Preferences getDefaultPreferences(Context context) {
        if (helper == null) {
            helper = new DatabaseHelper(context);
        }

        return helper.getPreferences(context.getPreferencesDir().getName());
    }

    /**
     * setLogLevel
     * @param context
     */
    public static void setLogLevel(Context context) {
        String levelValue = AppConfig.getString(context, AppConfig.Key.LOG_LEVEL);

        if (levelValue == null) {
            levelValue = "";
        }

        switch (levelValue) {
            case "VERBOSE" : SLog.setLevel(SLog.LEVEL_VERBOSE);break;
            case "DEBUG" : SLog.setLevel(SLog.LEVEL_DEBUG);break;
            case "INFO" : SLog.setLevel(SLog.LEVEL_INFO);break;
            case "ERROR" : SLog.setLevel(SLog.LEVEL_ERROR);break;
            case "WARNING" : SLog.setLevel(SLog.LEVEL_WARNING);break;
            case "NONE" : SLog.setLevel(SLog.LEVEL_NONE);break;
            default: break;
        }
    }

    public enum Key {
        SCROLLING_PAUSE_LOAD("PREFERENCE_SCROLLING_PAUSE_LOAD", false),
        SHOW_IMAGE_DOWNLOAD_PROGRESS("PREFERENCE_SHOW_IMAGE_DOWNLOAD_PROGRESS", false),
        MOBILE_NETWORK_PAUSE_DOWNLOAD("PREFERENCE_MOBILE_NETWORK_PAUSE_DOWNLOAD", true),
        SHOW_IMAGE_FROM_FLAG("PREFERENCE_SHOW_IMAGE_FROM_FLAG", false),
        CLICK_RETRY_ON_PAUSE_DOWNLOAD("PREFERENCE_CLICK_DISPLAY_ON_PAUSE_DOWNLOAD", true),
        CLICK_RETRY_ON_FAILED("PREFERENCE_CLICK_DISPLAY_ON_FAILED", true),
        CLICK_SHOW_PRESSED_STATUS("PREFERENCE_CLICK_SHOW_PRESSED_STATUS", true),
        GLOBAL_DISABLE_CACHE_IN_MEMORY("PREFERENCE_GLOBAL_DISABLE_CACHE_IN_MEMORY", false),
        GLOBAL_DISABLE_CACHE_IN_DISK("PREFERENCE_GLOBAL_DISABLE_CACHE_IN_DISK", false),
        GLOBAL_DISABLE_BITMAP_POOL("PREFERENCE_GLOBAL_DISABLE_BITMAP_POOL", false),
        GLOBAL_LOW_QUALITY_IMAGE("PREFERENCE_GLOBAL_LOW_QUALITY_IMAGE", false),
        GLOBAL_IN_PREFER_QUALITY_OVER_SPEED("PREFERENCE_GLOBAL_IN_PREFER_QUALITY_OVER_SPEED", false),
        SUPPORT_ZOOM("PREFERENCE_SUPPORT_ZOOM", true),
        READ_MODE("PREFERENCE_READ_MODE", true),
        THUMBNAIL_MODE("PREFERENCE_THUMBNAIL_MODE", true),
        LOCATION_ANIMATE("PREFERENCE_LOCATION_ANIMATE", true),
        CACHE_PROCESSED_IMAGE("PREFERENCE_CACHE_PROCESSED_IMAGE", true),
        DISABLE_CORRECT_IMAGE_ORIENTATION("PREFERENCE_DISABLE_CORRECT_IMAGE_ORIENTATION", false),
        PAUSE_BLOCK_DISPLAY_WHEN_PAGE_NOT_VISIBLE("PREFERENCE_PAUSE_BLOCK_DISPLAY_WHEN_PAGE_NOT_VISIBLE", true),
        FIXED_THREE_LEVEL_ZOOM_MODE("PREFERENCE_FIXED_THREE_LEVEL_ZOOM_MODE", false),
        PLAY_GIF_ON_LIST("PREFERENCE_PLAY_GIF_ON_LIST", false),
        SHOW_GIF_FLAG("PREFERENCE_SHOW_GIF_FLAG", true),
        LOG_LEVEL("PREFERENCE_LOG_level", false),
        LOG_REQUEST("PREFERENCE_LOG_REQUEST", BuildConfig.DEBUG),
        LOG_CACHE("PREFERENCE_LOG_CACHE", false),
        LOG_ZOOM("PREFERENCE_LOG_ZOOM", false),
        LOG_ZOOM_BLOCK_DISPLAY("LOG_ZOOM_BLOCK_DISPLAY", false),
        LOG_TIME("PREFERENCE_LOG_TIME", false),
        SHOW_TOOLS_IN_IMAGE_DETAIL("PREFERENCE_SHOW_TOOLS_IN_IMAGE_DETAIL", false),
        OUT_LOG_2_SDCARD("PREFERENCE_OUT_LOG_2_SDCARD", false),
        CLICK_PLAY_GIF("PREFERENCE_CLICK_PLAY_GIF", false),
        SHOW_UNSPLASH_RAW_IMAGE("PREFERENCE_SHOW_UNSPLASH_RAW_IMAGE", false),
        LONG_CLICK_SHOW_IMAGE_INFO("LONG_CLICK_SHOW_IMAGE_INFO", false),
        SHOW_ROUND_RECT_IN_PHOTO_LIST("SHOW_ROUND_RECT_IN_PHOTO_LIST", false);

        private String keyName;
        private boolean isDefaultValue;

        Key(String keyName, boolean isDefaultValue) {
            this.keyName = keyName;
            this.isDefaultValue = isDefaultValue;
        }
    }
}
